package com.srpingde.hhh;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.TextView;
import android.widget.Toast;

import com.springde.hhh.R;
import com.srpingde.hhh.application.HhhApp;
import com.srpingde.hhh.http.AsyncHttpResponseHandler;
import com.srpingde.hhh.jiekou.HHHIml;
import com.srpingde.hhh.jiekou.HHHInterface;

public class RegisterActivity extends Activity {
	EditText name,password,queRenPassword;
	ImageView back;
	LinearLayout register_ly;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        name = (EditText) findViewById(R.id.register_name);
        name.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = name.getSelectionStart();
                selectionEnd = name.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    name.setText(s);
                    name.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(RegisterActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
        password = (EditText) findViewById(R.id.register_password);
        password.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = password.getSelectionStart();
                selectionEnd = password.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    password.setText(s);
                    password.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(RegisterActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
        
        
        queRenPassword = (EditText) findViewById(R.id.register_queren);
        queRenPassword.addTextChangedListener(new TextWatcher() {
            private CharSequence temp;
            private int selectionStart;
            private int selectionEnd;
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				 temp = s;

			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				selectionStart = queRenPassword.getSelectionStart();
                selectionEnd = queRenPassword.getSelectionEnd();
                if (temp.length() > 10) {
                    s.delete(selectionStart - 1, selectionEnd);
                    int tempSelection = selectionEnd;
                    queRenPassword.setText(s);
                    queRenPassword.setSelection(tempSelection);//设置光标在最后
                    HhhApp.getInstance().showDialog(RegisterActivity.this, "长度为10个字符！");
                }
				
				
			}
		});
        back = (ImageView) findViewById(R.id.back);
        register_ly = (LinearLayout) findViewById(R.id.register_ly);
        layout();
    }
    public void onClick(View v){
    	switch (v.getId()) {
		case R.id.back:
			finish();
			break;

		case R.id.register_queren_btn:
			if(!name.getText().toString().equals("") && !password.getText().toString().equals("") && !queRenPassword.getText().toString().equals("")){
		    	if(name.getText().toString().length()>10)
		    	{
		    		HhhApp.getInstance().showDialog(RegisterActivity.this, "长度为10个字符！");
		    		return;
		    	}
		    	if(password.getText().toString().length()>10)
		    	{
		    		HhhApp.getInstance().showDialog(RegisterActivity.this, "长度为10个字符！");
		    		return;
		    	}
				if(password.getText().toString().equals(queRenPassword.getText().toString())){
		    		final HHHIml weiQuIml = new HHHIml();
		    		String url1=HHHInterface.httpHeader + "user.php?act=validate&name="+name.getText().toString().trim();
		    		weiQuIml.loginHttp(RegisterActivity.this,url1, new AsyncHttpResponseHandler(){
						@Override
						public void onSuccess(String content) {
							// TODO Auto-generated method stub
							super.onSuccess(content);
							Log.e("mytag", content);
							if(content != null){
								try {
									final JSONObject jsonObject = new JSONObject(content);
									if(jsonObject.getBoolean("validate")){
										HhhApp.getInstance().showDialog(RegisterActivity.this, "注册失败，用户名已经存在");
									}else{
							    		String url = HHHInterface.httpHeader + "api.php?act=reguser&account="+name.getText().toString().trim()+"&pwd="+password.getText().toString().trim();
							    		Log.e("mytag", url);
							    		weiQuIml.loginHttp(RegisterActivity.this,url, new AsyncHttpResponseHandler(){

													@Override
													public void onSuccess(String content) {
														// TODO Auto-generated method stub
														super.onSuccess(content);
														Log.e("mytag", content);
														if(content != null){
															try {
																final JSONObject jsonObject = new JSONObject(content);
																if(jsonObject.getBoolean("return")){
																	new AlertDialog.Builder(RegisterActivity.this)
																	.setTitle("提示")
																	.setMessage("注册成功").setPositiveButton("确定", new DialogInterface.OnClickListener() {
																		@Override
																		public void onClick(DialogInterface dialog, int which) {
																			// TODO Auto-generated method stub
																			try {
																				HhhApp.getInstance().setUid(jsonObject.getString("uid"));
																				HhhApp.getInstance().setUname(name.getText().toString());
																				LoginActivity.getInstance().finish();
																				finish();
																			} catch (JSONException e) {
																				// TODO Auto-generated catch block
																				e.printStackTrace();
																			}
//																			
																		}
																	}).create().show();
																}else{
																	HhhApp.getInstance().showDialog(RegisterActivity.this, "注册失败，用户名已经存在");
																}
															} catch (Exception e) {
																// TODO Auto-generated catch block
																e.printStackTrace();
															}
														}else{
															HhhApp.getInstance().showDialog(RegisterActivity.this, "注册失败");
														}
													}
											});
									}
								} catch (Exception e) {
									// TODO Auto-generated catch block
									e.printStackTrace();
								}
							}else{
								HhhApp.getInstance().showDialog(RegisterActivity.this, "注册失败");
							}
						}
				});
		    	}else{
		    		new AlertDialog.Builder(RegisterActivity.this)
		    		.setTitle("提示")
		    		.setMessage("密码和确认密码不一致").setPositiveButton("确定", null).create().show();
		    	}
		    }else{
	    		new AlertDialog.Builder(RegisterActivity.this)
	    		.setTitle("提示")
	    		.setMessage("用户名和密码不能为空").setPositiveButton("确定", null).create().show();
	    	}
			break;
		}
    }
    private void layout() {
    	//输入框和文字的padding值
    	int paddingWidth_tv = (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105);
    	//int paddingWidth = (int)(WeiQuApp.getInstance().getDisplayHightAndWightPx()[1] * 0.15);
    	//name.setPadding(paddingWidth, 0, paddingWidth, 0);
    	//password.setPadding(paddingWidth, 0, paddingWidth, 0);
    	//queRenPassword.setPadding(paddingWidth, 0, paddingWidth, 0);
    	//((TextView)findViewById(R.id.register_name_tv)).setPadding(paddingWidth_tv, 0, 0, 0);
    	//((TextView)findViewById(R.id.register_password_tv)).setPadding(paddingWidth_tv, 0, 0, 0);
    	//((TextView)findViewById(R.id.register_querenpassword_tv)).setPadding(paddingWidth_tv, 0, 0, 0);
		// 头的高度
		TextView top_title_tv = (TextView) findViewById(R.id.top_title);
		RelativeLayout.LayoutParams titleLayoutParams = (android.widget.RelativeLayout.LayoutParams) top_title_tv
				.getLayoutParams();
		titleLayoutParams.height = HhhApp.getInstance().title_h();
		top_title_tv.setLayoutParams(titleLayoutParams);
		//back
		RelativeLayout.LayoutParams backLayoutParams = (LayoutParams) back.getLayoutParams();
		backLayoutParams.height = HhhApp.getInstance().title_h();
		back.setLayoutParams(backLayoutParams);
		
		//editLayout(name);
		//editLayout(password);
		//editLayout(queRenPassword);
		RelativeLayout.LayoutParams login_lyLayoutParams = (LayoutParams) register_ly.getLayoutParams();
    	login_lyLayoutParams.setMargins(paddingWidth_tv, paddingWidth_tv, paddingWidth_tv, 0);
    	register_ly.setLayoutParams(login_lyLayoutParams);
    	
    	Button button = (Button) findViewById(R.id.register_queren_btn);
    	RelativeLayout.LayoutParams buttonLayoutParams = (LayoutParams) button.getLayoutParams();
    	buttonLayoutParams.setMargins(paddingWidth_tv, paddingWidth_tv, paddingWidth_tv, 0);
    	button.setLayoutParams(buttonLayoutParams);
	}
    private void editLayout(EditText editText){
    	LinearLayout.LayoutParams layoutParams = (android.widget.LinearLayout.LayoutParams) editText.getLayoutParams();
    	layoutParams.setMargins((int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105), 0, (int)(HhhApp.getInstance().getDisplayHightAndWightPx()[1] * 0.105), 0);
    	editText.setLayoutParams(layoutParams);
    }
}
